package com.pengtu.gsj.service.impl.check.gcjs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.gcjs.StandardManagementDao;
import com.pengtu.gsj.dao.system.AttachmentDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.gcjs.StandardManagement;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.gcjs.RuleManagerService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class RuleManagerServiceImpl implements RuleManagerService{
	@Autowired
	private AttachmentDao attachmentDao;
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private StandardManagementDao standardManagementDao;
	
	@Override
	public List<StandardManagement> getStandardManagements(QueryFilter queryFilter) {
		return standardManagementDao.find(queryFilter);
	}

	@Override
	public List<StandardManagement> getRuleManagerProject(QueryFilter queryFilter) {
		return standardManagementDao.find(queryFilter, "testYear", true);
	}

	@Override
	public Map<String, Object> getRulemanagers(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> values = new ArrayList<Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<Double> scoreLists = new ArrayList<Double>();
		List<StandardManagement> standLists = new ArrayList<StandardManagement>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String itemNameIds = (String)jsonObject.get("itemProjectId");
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double reduceScore = Double.parseDouble((String)jsonObject.get("yearPart"));
		double percentage =0; 			 //系统完善度
		double total = 0;			   	  //检查总分
		double tableFull = yearFull * 4;   //表总分
		
		//根据tag标签和时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		if (StringUtils.isNotEmpty(itemNameIds)) {
			//当检索条件不为空的时候更新分数
			values.add(itemNameIds);
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = itemNameIds.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				//根据tag检索是否有附件
				if( i > 3) {
					break;
				}
				StandardManagement stManagement = standardManagementDao.get(idArrs[i]);
				standLists.add(stManagement);
				//定义一个变量来保存分数
				double score = 0;
				//记录是否扣分，为空的扣分，不为空的不扣分
				score += ("0".equals(stManagement.getPlaning())) ? reduceScore : 0;
				score += ("0".equals(stManagement.getDesign())) ? reduceScore : 0;
				score += ("0".equals(stManagement.getBidding())) ? reduceScore : 0;
				score += ("0".equals(stManagement.getProjectQuality())) ? reduceScore : 0;
				score += ("0".equals(stManagement.getWorkReport())) ? reduceScore : 0;
				score += ("0".equals(stManagement.getAcceptanceStandard())) ? reduceScore : 0;
				score += ("0".equals(stManagement.getChangeRule())) ? reduceScore : 0;
				//计算单个项目总分，并保存到实体里面
				score = yearFull - score > 0 ? yearFull - score : 0;
				scoreLists.add(score);
				total += score;
			}
			//目标对象全部遍历后，可以得到总分total并算出完善度
			percentage = MathsUtils.divDouble(total*100, tableFull, 2);
			//添加一个标记，确定前端是否输出表格和在空条件检索并导出时是否输出表头
			map.put("flag",true);
		}
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("percent", percentage);
		map.put("scoreLists", scoreLists);
		map.put("manageData", standLists);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}

	/**
	 * 根据startTime, endTime, id检索数据文件列表
	 */
	@Override
	public List<StandardManagement> getCheckData(String startTime, String endTime, String id) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("testYear", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("testYear",  endTime, MatchType.LE);
		}
		if (StringUtils.isNotEmpty(id)) {
			queryFilter.addFilter("id", id, MatchType.EQ);
		}
		return standardManagementDao.find(queryFilter);
		}

	/**
	 * 根据startTime, endTime检索数据文件列表
	 */
	@Override
	public List<StandardManagement> getCheckData(String startTime, String endTime) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("testYear", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("testYear",  endTime, MatchType.LE);
		}
		return standardManagementDao.find(queryFilter);
	}
}

